package com.woniu.controller;

import com.woniu.been.entity.News;
import com.woniu.been.entity.vo.NewsVo;
import com.woniu.service.NewsService;
import com.woniu.utils.PageResult;
import com.woniu.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/NewsController")
@CrossOrigin
public class NewsController {
    @Autowired
    private NewsService newsService;

    /*
    * 分页查询所有公告信息
    * */
    @PostMapping("findAllNews")
    public ResponseResult<PageResult> findAllNews(@RequestBody NewsVo NewsVo) {
        System.out.println("----------"+NewsVo);
        PageResult list = newsService.findAllNews(NewsVo,NewsVo.getPageNum(), NewsVo.getPageSize());
        return ResponseResult.success(list);
    }

    /*
    * 查询所有操作员名称
    * */

    @DeleteMapping("deleteNews")
    public ResponseResult<News> deleteNews(Integer id){
        newsService.deleteNews(id);
        return ResponseResult.success();
    }

    /*
    * 查询所有操作员名称
    * */

    @GetMapping("queryOperNameList") // /api/NewsController/queryOperNameList
    public ResponseResult<List<String>> queryOperNameList(){
        List<String> list = newsService.queryOperNameList();
        System.out.println(list);
        return ResponseResult.success(list);
    }

    /*
    * 动态添加公告信息
    * */
    @PostMapping("insertNews")
    public ResponseResult<News> insertNews(@RequestBody News sysNews){
        newsService.insertNews(sysNews);
        return ResponseResult.success();
    }


    /*
    * 根据id编辑公告信息
     * */
    @PutMapping("updateNewsById")
    public ResponseResult<News> updateNewsById(@RequestBody News sysNews){
        System.out.println(sysNews);
        newsService.updateNewsById(sysNews);
        return ResponseResult.success();
    }

    /*
    * 根据id查询某一条公告信息
    * */
    @GetMapping("findNewsByid")
    public ResponseResult<News> findNewsByid(Integer id){
        System.out.println("new id :"+id);
        return ResponseResult.success(newsService.findNewsById(id));
    }
}
